<?php
$k = 0;
$user_odd = Frontend::findUserOddByUid((int) $_SESSION["user_id"], "desc");
?>
<div class="span12">
    <legend>Danh sách dự đoán</legend>
<!--    <select id="selected">
        <option value="0">Dự đoán bóng đá</option>
        <option value="1">Dự đoán sổ xố</option>
    </select>-->
    <div id="change">
        <?php if (iterator_to_array($user_odd)) { ?>
            <table class="table table-bordered table-striped table-heading">
                <thead>
                    <tr class="heading">
                        <th>Trận đấu</th>
                        <th>Chọn</th>
                        <th>Kiểu</th>
                        <th>Tiền đặt</th>
                        <th>Ngày dự đoán</th>
                        <th>Trạng thái</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($user_odd as $_user_odd) :
                        $match = iterator_to_array(Frontend::findMatchById((int) $_user_odd["match_id"]));
                        $team1 = iterator_to_array(Frontend::findTeamById((int) $match[$_user_odd["match_id"]]["Team1"]));
                        $team2 = iterator_to_array(Frontend::findTeamById((int) $match[$_user_odd["match_id"]]["Team2"]));
                        ?>
                        <tr>
                            <td><?php echo $team1[$match[$_user_odd["match_id"]]["Team1"]]["name"] . " - " . $team2[$match[$_user_odd["match_id"]]["Team2"]]["name"] ?></td>
                            <?php
                            $odd = Frontend::findOddById((int) $_user_odd["oddid"]);
                            foreach ($odd as $_odd) {
                                ?>
                                <td>
                                    <?php
                                    $value = explode("|", $_odd["value"]);
                                    if (array_key_exists($_odd["typeID"], $arr_odd_bets["ODD_BETS"])) {
                                        if (count($arr_odd_bets["ODD_BETS"][$_odd["typeID"]]) == 2) {
                                            if ($_user_odd["value"] > 2) {
                                                if ($_user_odd["value"] % 2 == 0) {
                                                    $k = 0;
                                                }
                                                if ($_user_odd["value"] % 2 == 1) {
                                                    $k = 1;
                                                }
                                            } else {
                                                $k = $_user_odd["value"];
                                            }
                                        }
                                        if (count($arr_odd_bets["ODD_BETS"][$_odd["typeID"]]) == 3) {
                                            if ($_user_odd["value"] > 3) {
                                                if ($_user_odd["value"] % 3 == 0) {
                                                    $k = 0;
                                                }
                                                if ($_user_odd["value"] % 3 == 1) {
                                                    $k = 1;
                                                }
                                                if ($_user_odd["value"] % 3 == 2) {
                                                    $k = 2;
                                                }
                                            } else {
                                                $k = $_user_odd["value"];
                                            }
                                        }
                                        if (count($arr_odd_bets["ODD_BETS"][$_odd["typeID"]]) == 4) {
                                            if ($_user_odd["value"] > 3) {
                                                if ($_user_odd["value"] % 3 == 0) {
                                                    $k = 0;
                                                }
                                                if ($_user_odd["value"] % 3 == 1) {
                                                    $k = 1;
                                                }
                                                if ($_user_odd["value"] % 3 == 2) {
                                                    $k = 2;
                                                }
                                                if ($_user_odd["value"] % 3 == 3) {
                                                    $k = 3;
                                                }
                                            } else {
                                                $k = $_user_odd["value"];
                                            }
                                        }
                                    }
                                    echo $arr_odd_bets["ODD_BETS"][$_odd["typeID"]][$k] . "<span style='margin-left: 10px' class='label label-warning'>{$value[$_user_odd["value"]]}</span>";
                                    ?>
                                </td>
                                <td><?php echo $_odd["title"] ?></td>
                            <?php } ?>
                            <td><?php echo $_user_odd["money"] ?></td>
                            <td><?php echo date("d / m / Y", $_user_odd["submitdate"]) ?></td>
                            <td><?php if($_user_odd["status"] == 0) echo "Chờ"; else echo "Đã xử lý"; ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php } else { ?>
            <div class="alert">
                Chưa có dự đoán nào.
            </div>
        <?php } ?>
    </div>
</div>
<script>
    $(document).ready(function() {
        $('#selected').change(function() {
            $.ajax({
                // The link we are accessing.
                url: "/ajax.php",
                // The type of request.
                type: "get",
                data: "act=change_danhsach&key=" + $('#selected').val(),
                // The type of data that is getting returned.
                dataType: "html",
                error: function() {
                },
                success: function(strData) {
                    // Load the content in to the page.
                    $("#change").html(strData);
                }
            });
        });
    });
</script>